<template>
    <div class="article-lists">
        <el-card class="!border-none" shadow="never">

            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="手机号">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.keys"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <el-button v-perms="['user.agent/addFreeAccount']" type="primary" class="mb-4" @click="addOpen">
                添加免费代理人
            </el-button>
            <el-button v-perms="['user.agent/inviteAgentLink']" type="primary" class="mb-4" @click="addLink">
                免费代理人邀请
            </el-button>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="ID" min-width="120" prop="id"/>
                <el-table-column label="代理人手机号" min-width="180" prop="phone">
                    <template #default="{row}">
                        <span class="txt">{{ row.phone }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" min-width="190" prop="created"/>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <el-dialog v-model="recordVisible" title="免费代理人邀请" width="600">
            <div class="flex">
                <div class="flex-left">活动链接</div>
                <div>
                    <div class="flex-num">点击链接的所有人都可以领</div>
                    <div class="flex-link">链接：{{ link }}<el-link class="flex-m" type="primary" icon="el-icon-document-copy" :underline="false" v-copy="link">复制</el-link></div>
                </div>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="()=>{recordVisible = false}">取消</el-button>
                    <el-button v-perms="['user.agent/setBondConfig']" type="primary" v-copy="link">复制链接</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup name="order">
import { freeList, addFreeAccount, inviteAgentLink} from '@/api/agent'
import { usePaging } from '@/hooks/usePaging'
import { ElMessageBox } from 'element-plus'
import type {FormInstance} from 'element-plus';
const formRef = shallowRef<FormInstance>()
import feedback from '@/utils/feedback'
const queryParams = reactive({
    keys: '',
    account: '',
    status: '',
    agent_group_id: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: freeList,
    params: queryParams
})
const addOpen = () => {
    ElMessageBox.prompt('请输入需要添加的手机号', '添加免费代理人', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        inputPattern: /^1[3-9]\d{9}$/,
        inputErrorMessage: '请输入正确的手机号',
    }).then(({ value }) => {
        addAgent(value)
    }).catch(() => {
      
    })
}
const recordVisible = ref(false);
const link = ref('');
const addLink = () => {
    recordVisible.value = true;
}
const addAgent = async (num:any) => {
    const res = await addFreeAccount({
        phone: num
    })
    feedback.msgSuccess('添加成功')
    getLists()
}
const getlink = async () => {
    const res = await inviteAgentLink({});
    console.log(res)
    link.value = res.invite_link;
}

onActivated(() => {
    getLists()
})
getlink()
getLists()


</script>
<style lang="scss" scoped>
.txt{
    color: var(--el-color-primary);
}
.flex {
    display: flex;
}
.flex-left {
    flex-shrink: 0;
}
.flex-m {
    margin-left: 5px;
}
.flex-num {
    margin-left: 20px;
    margin-bottom: 20px;
    color: #999;
}
.flex-link {
    margin-left: 20px;
    margin-bottom: 20px;
}
.tip {
    color: #f56c6c;
}
</style>